from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import redirect, render


class JudgeLoginMiddleware(MiddlewareMixin):
    """检测登录"""

    def process_request(self, request):
        # 判断访问的是否为不登录可以访问的页面
        no_login = ["/user_login/", "/", "/register/", "/image/code/", "/post/", "/admin/", "admin/login/",
                    "/admin/post/"]
        if request.path_info in no_login:
            return

        # 利用session判断是否登录
        user_info = request.session.get("info")
        if user_info:
            return
        return redirect('user_login')
